package com.syl.controller;

import com.syl.bean.RequestMsg;
import com.syl.bean.VisitedUrl;
import com.syl.dao.VisitedUrlDao;
import com.syl.util.DateUtil;
import com.syl.util.DbHelp;
import com.syl.util.HttpCommon;
import com.syl.util.JsonUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by syl on 2016/6/11.
 * 入口控制器
 */
public class Entrance {
    private Map<String,String> yet_url = new HashMap<String,String>();//已访问的url
    private Map<String,String> error_url = new HashMap<String,String>();//网络异常的url
    private Queue wait_url = new LinkedList();//待访问的url
    private HttpCommon hc;

    public Entrance(){

    }

    public Entrance(String url,int wait_time,String encoding){
        hc = new HttpCommon(wait_time,encoding);
        String html = hc.send_get_request(url, null).html;
        get_shape_a(html,url);
        again_again();
        System.out.println("待访问的url数"+wait_url.size ());
    }

    /**
     * 挑选a标签
     * @param html
     * @param base_url 基础url
     */
    public void get_shape_a(String html,String base_url){
        Document doc = Jsoup.parse(html);
        Elements elements = doc.select("a");
        for(Element e:elements){
            String href = e.attr("href");
//            if(href.indexOf("html/gndy/jddy/20160320/50541.html") > -1){
//                System.out.println(href+"   ************************************************"+e.text()+" *** "+base_url);
//            }
            StringBuffer sb = new StringBuffer(base_url);
            if(in_station(href)){//如果是站内的资源才加入队列
                href = href.substring(0,1).equals("/") ? href : "/"+href;
                sb.append(href);
                wait_url.offer(sb.toString());
            }else if(in_paging(href,e)){ // 如果是分页 页 也加入队列
                String[] split =  sb.toString().split("/");
                sb = new StringBuffer();
                for (int i = 0; i <split.length ; i++) {
                    if(i < split.length-1){
                        sb.append(split[i]);
                        sb.append("/");
                    }
                    else sb.append(href);
                }
                wait_url.offer(sb.toString());
            }
        }
        //System.out.println("待执行数"+wait_url.size());
    }

    /**
     *
     */
    public void again_again(){
        while (true) {
            Object temp_url = wait_url.peek();
            if (temp_url == null) {
                save_yet_url();// 没有待访问的url 时保存所有已访问过的信息
                break;
            }
            String url = temp_url.toString();
            if (!yet_url.containsKey(url)) {//如果是已经访问过的url 则下一个
                RequestMsg msg = hc.send_get_request(url, null);
                if (!msg.live) {
                    error_url.put(url, "1");
                    System.err.println(url+"   =========================================" +msg.html);
                    continue;
                }//如果网络错误则继续下一个
                String html = msg.html;
                System.out.println("成功访问 : "+url+" ");
                yet_url.put(url, set_yet_data(html));
                if (html.indexOf("<font color=\"red\">影片地址：</font>") > -1) {
                    start_extract(html);//如果是下载页面就提取内容
                }
                else get_shape_a(html,url);//不是 下载页面 继续寻找
            }
            wait_url.poll();
            //System.out.println("待访问的url"+wait_url.size());
        }
    }

    /**
     * @param html  页面html
     * @return 需要保存的已访问 数据
     */
    private String set_yet_data(String html){
        Document doc = Jsoup.parse(html);
        String title = doc.select("title").html();
        Map<String,String> map = new HashMap<String, String>();
        map.put("title",title);
        map.put("createTime", DateUtil.get4yMdHms()+"");
        return JsonUtil.getJson(map);
    }

    /**
     * 开始提取有用的信息
     */
    public void start_extract(String html){
        Document doc = Jsoup.parse(html);
        Elements main_div = doc.select(".bd3r .co_area2");

        String all_title = main_div.select("h1").text(); //全部标题
        Pattern pattern = Pattern.compile("(?<=《).*(?=》)");
        Matcher matcher=pattern.matcher(all_title);
        StringBuffer buffer = new StringBuffer();// 标题
        while(matcher.find()){
            buffer.append(matcher.group());
        }
        System.out.println("影片名称 :"+buffer.toString());

        Elements imgs = main_div.select("img");// 所有图片
        for(Element img:imgs){
            String href = img.attr("src");
            System.out.println("图片地址"+href);
        }
        Elements texts = main_div.select("#Zoom").select("span").eq(0).select("p"); // 所有文字说明
        System.out.println(texts.text());

        Elements a = main_div.select("table").eq(0).select("a");
        String thunder_href = a.attr("href"); // 下载地址
        System.out.println("下载地址 : "+thunder_href);


    }

    /**
     * 保存已访问过的url
     */
    private void save_yet_url(){
        System.out.println("没有待访问的了");
        System.out.println("网络失败的连接数 :"+error_url.size());
        DbHelp db = new DbHelp();
        VisitedUrlDao dao = db.getDao(VisitedUrlDao.class);
        Set<String> keys = yet_url.keySet();
        for (String key:keys){
            VisitedUrl bean = new VisitedUrl();
            bean.setUrl(key);
            Map<String, String> temp = JsonUtil.getMapStr(yet_url.get(key));
            try {
                bean.setTitle(new String(temp.get("title").getBytes(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            bean.setCreateTime(temp.get("createTime"));

            System.out.println(bean);
            //dao.insertData(bean);
        }
        //db.commit();//提交
    }

    /**
     * 是否站内的地址
     * @param href
     * @return
     */
    private boolean in_station(String href){
        Pattern pattern = Pattern.compile("^/*html/.*");
        Matcher matcher = pattern.matcher(href);
        return matcher.matches();
    }

    /**
     * 是否是站内  并且是分页地址
     * @param href
     * @return
     */
    private boolean in_paging(String href,Element e){
        String text = e.text();
        if(href.indexOf("index") > -1)return false;
        Pattern pattern0 = Pattern.compile("^(首页|末页|上一页|下一页|网站地图)$");
        if(pattern0.matcher(text).matches())return false;

        Pattern pattern1 = Pattern.compile("^(?!.*?www).*\\.html$");
        Matcher matcher = pattern1.matcher(href);
        return matcher.matches();
    }

}
